<?php
//require_once 'Zend/Feed.php';
require_once 'Zend/Date.php';

/**
 * Classe che carica un Feed RSS generico su un array normalizzandolo in modo da semplificarne la lettura
 * @version 1.3.1
 * @author e-xtrategy 2008-2009
 */
class Rolando_Feed {
	
	const DELICIUS = 'Delicius';	
	const PIPES = 'Pipes';
	const TWITTER = 'Twitter';
	const UPCOMING = 'Upcoming';
	const USTREAM = 'Ustream';
	const NINGFOTO = 'NingFoto';

	/**
	 * Oggetto Zend_Feed
	 *
	 * @var Zend_Feed
	 */
	public $oRss;
	
	
	/**
	 * Metodo statico che istanzia l'oggetto giusto 
	 *
	 * @param string $strUrlFeed
	 * @param string $bTipo
	 * @return Rolando_Feed|null
	 */
	static public function getFeed($strUrlFeed,$tipoServizio='') {
		if (in_array($tipoServizio,array(Rolando_Feed::DELICIUS,
		                                 Rolando_Feed::PIPES,
		                                 Rolando_Feed::TWITTER,
		                                 Rolando_Feed::UPCOMING,
		                                 Rolando_Feed::USTREAM,
		                                 Rolando_Feed::NINGFOTO))) {
			$strObj = 'Rolando_Feed'.$tipoServizio;
			return new $strObj($strUrlFeed);
		} else {
			return null;
		}
	}
	
	
	/**
	 * costruttore della classe setta la proprietà e la importa
	 *
	 * @param string $strUrlFeed
	 * @return Rolando_Feed
	 */
	public function __construct($strUrlFeed){
		$this->strUrlFeed = $strUrlFeed;
		$this->importaFeed($strUrlFeed);
	}
	

	
	/**
	 * Legge in contenuto del Feed tramite Zend_Feed
	 *
	 * @param string $strUrlFeed
	 */
	private function importaFeed($strUrlFeed=null){
		$strUrlFeed = (is_null($strUrlFeed)) ? $this->strUrlFeed : $strUrlFeed;
		try {
			$this->oRss = Zend_Feed::import($strUrlFeed);    
		} catch (Zend_Feed_Exception $e) {
	    	// feed import failed
	    	echo "Exception caught importing feed: {$e->getMessage()}\n";
		}
	}
	
	
	/**
	 * Normalizza il feed letto e lo filtra in base al parametro
	 * @param integer $nItems
	 * @return array
	 */
	function normalizzaFeed($nItems=0){
		// Initialize the channel data array
		$channel = array('TitoloFeed' => $this->oRss->title(),
										 'LinkFeed'   => $this->oRss->link(),
										 'aItems'     => array());			 
    	$this->aFeed = $this->normalizzaItems($channel,$nItems);				 
		return $this->aFeed;
	}
	
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			/* @var $item Zend_Feed_Entry */
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->getIso();
			$channel['aItems'][] = array('Titolo' 		 => $item->title(),
										'Descrizione' => $item->description(),
										'Link'   		 => $item->link('alternate'),
										'Data' 			 => $strData);
		 }
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
}//fine classe




/**
 * Classe Rolando_Feed dedicata a Delicious
 *
 */
class Rolando_FeedDelicius extends Rolando_Feed {
	
	/**
	 * costruttore della classe richiama il costruttore del parent
	 *
	 * @param string $strUrlFeed
	 */
	public function __construct($strUrlFeed){
		parent::__construct($strUrlFeed);
	}
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->getIso();
			$channel['aItems'][] = array('Titolo'		=> $item->title(),
										'Descrizione'	=> $item->description(),
										'Link'			=> $item->link('alternate'),
										'Data'			=> $strData);
		 }
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
}//fine classe

/**
 * Classe Rolando_Feed dedicata a Twitter
 *
 */
class Rolando_FeedTwitter extends Rolando_Feed {
	
	/**
	 * costruttore della classe richiama il costruttore del parent
	 *
	 * @param string $strUrlFeed
	 */
	public function __construct($strUrlFeed){
		parent::__construct($strUrlFeed);
	}
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->get(Zend_Date::TIMESTAMP);
			$channel['aItems'][] = array('Titolo'		=> $item->title(),
										'Descrizione'	=> $item->description(),
										'Link'			=> $item->link('alternate'),
										'Data'			=> $strData,
										'Tipo'			=> 'Twitter');
		 }
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
}//fine classe

/**
 * Classe Rolando_Feed dedicata a Upcoming
 *
 */
class Rolando_FeedUpcoming extends Rolando_Feed {
	
	/**
	 * costruttore della classe richiama il costruttore del parent
	 *
	 * @param string $strUrlFeed
	 */
	public function __construct($strUrlFeed){
		parent::__construct($strUrlFeed);
	}
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			//var_dump($item->saveXml());
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->get(Zend_Date::TIMESTAMP);
			$channel['aItems'][] = array('Titolo'		=> $item->title(),
										'Descrizione'	=> $item->description(),
										'Link'			=> $item->link('alternate'),
										'Data'			=> $strData,
										'Tipo'			=> 'Upcoming');
		 }
		 //var_dump($channel);
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
}//fine classe


/**
 * Classe Rolando_Feed dedicata a Ustream
 *
 */
class Rolando_FeedUstream extends Rolando_Feed {
	
	/**
	 * costruttore della classe richiama il costruttore del parent
	 *
	 * @param string $strUrlFeed
	 */
	public function __construct($strUrlFeed){
		parent::__construct($strUrlFeed);
	}
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			//var_dump($item->saveXml());
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->get(Zend_Date::TIMESTAMP);
			$strthumbnails = substr($item->description(),strpos($item->description(),'Thumbnail Url: ')+strlen('Thumbnail Url: '));
			$strthumbnails = substr($strthumbnails,0,strpos($strthumbnails,'jpg')+strlen('jpg'));
			$channel['aItems'][] = array('Titolo'		=> $item->title(),
										'Descrizione'	=> $item->description(),
										'Link'			=> $item->link('alternate'),
										'Data'			=> $strData,
										'thumbnails'	=> $strthumbnails,
										'Tipo'			=> 'Ustream');
		 }
		 //var_dump($channel);
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
}//fine classe

/**
 * Estensione della classe dedicata PIPES
 *
 */
class Rolando_FeedPipes extends Rolando_Feed {
	
	public function __construct($strUrlFeed){
		parent::__construct($strUrlFeed);
	}
	
	/**
	 * Normalizza i singoli Items
	 *
	 * @param array $channel
	 * @return array
	 */
	protected function normalizzaItems($channel,$nItems=0){
		foreach ($this->oRss as $item) {
			//var_dump($item->saveXml());
			$oDate = new Zend_Date($item->pubDate(),null,'en_US');
			$strData = $oDate->getIso();
			$channel['aItems'][] = array('Titolo'		=> substr($item->title(),4), //tolgo il prefisso del tipo
										'Descrizione'	=> $item->description(),
										'Link'			=> $item->link('alternate'),
										'Data'			=> $strData,
										'Tipo'			=> $this->estraiTipo($item));
		 }
		 if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
		 return $channel; 													 
	}
	
	/**
	 * definisce il tipo in base al prefisso su titolo (notazione e-x per il pipes)
	 *
	 * @param Zend_Feed_Element $item
	 * @return string
	 */
	private function estraiTipo($item) {
		switch (substr($item->title(),0,4)) {
			case "[tw]":
				return 'Twitter';	
			break;
			case "[dl]":
				return 'Delicious';	
			break;
			case "[uc]":
				return 'Upcoming';	
			break;
			case "[fk]":
				return 'Flickr';	
			break;
			case "[ss]":
				return 'Slideshare';	
			break;
			case "[yt]":
				return 'Youtube';	
			break;
			case "[us]":
				return 'Ustream';	
			break;
			default:
				return 'Feed';
			break;
		}
	}
	
}//fine classe

/**
 * Classe Rolando_Feed dedicata a NingFoto
 *
 */
class Rolando_FeedNingFoto extends Rolando_Feed {
    
    /**
     * costruttore della classe richiama il costruttore del parent
     *
     * @param string $strUrlFeed
     */
    public function __construct($strUrlFeed){
        parent::__construct($strUrlFeed);
    }
    
    /**
     * Normalizza i singoli Items
     *
     * @param array $channel
     * @return array
     */
    protected function normalizzaItems($channel,$nItems=0){
        foreach ($this->oRss as $item) {
            //print_r($item->saveXml());
            $oDate = new Zend_Date($item->pubDate(),null,'en_US');
            $strData = $oDate->get(Zend_Date::TIMESTAMP);
            //$strthumbnails = substr($item->description(),strpos($item->description(),'Thumbnail Url: ')+strlen('Thumbnail Url: '));
            //$strthumbnails = substr($strthumbnails,0,strpos($strthumbnails,'jpg')+strlen('jpg'));
            /*
             * @var Zend_Feed_Entry_Rss $item
             */
        	//print_r($item->media->thumbnail);
            foreach ($item->description as $description) {
                if ($description->prefix == 'media') {
                    /* title node within the media namespace */
                } else {
                	//echo ">>>".$description;
                	//echo ">>>".$description->_toString();
                	//echo ">>>".$description->__toString();
                	$strDescription = $description->__toString();
                	break;
                }
            }
        	
            $channel['aItems'][] = array('Titolo'       => $item->title(),
                                        'Descrizione'   => $strDescription,
                                        'Link'          => $item->link(),
                                        'Data'          => $strData,
                                        'thumbnails'    => $item->thumbnail['url'],
                                        'Tipo'          => 'NingFoto');
         }
         //var_dump($channel);
         if ($nItems != 0) $channel['aItems'] = array_slice($channel['aItems'],0,$nItems);
         return $channel;                                                    
    }
    
}//fine classe
